import {getCookie, removeCookie} from '@/utils/auth'
import {userLogout} from '@/apis/login/LoginApi'

const state = {
  // userInfo: getSession('userInfo') ? JSON.parse(getSession('userInfo')) : null,
  userInfo: getCookie('userInfo') ? JSON.parse(getCookie('userInfo')) : null,
  Log: [], //用户登录 0
  Offline: [],//用户下线 -1
  NotTested: [], //未测试 1 ，2
  appInfoData: "", //x6数据
  fappId: "", //应用ID
}

const mutations = {
  setUserInfo(state, data) {
    state.userInfo = data
  },
  webSocketMsg: (state, data) => {
    switch (data.type) {
      case 0:
        state.Log = data
        break;
      case -1:
        state.Offline = data
        break;
      default:
        //暂时默认未测试
        state.NotTested = data
        break;
    }
  },
  appInfoData: (state, data) => {
    state.appInfoData = data
  },
  fappId: (state, data) => {
    state.fappId = data
  },
}

const actions = {
  getUserInfo() {
    return new Promise((resolve, reject) => {
      let userInfo = getCookie('userInfo') ? JSON.parse(getCookie('userInfo')) : null
      resolve(userInfo)
    })
  },
  setUserInfo({commit}, userInfo) {
    return new Promise((resolve, reject) => {
      commit('setUserInfo', userInfo)
      resolve(userInfo)
    })
  },
  // 退出登录
  loginOut({commit, dispatch}) {
    return new Promise((resolve, reject) => {
      userLogout().then((res) => {
        // if (res.data.code === 2) {
        //   removeCookie();
        //   dispatch('permission/resetRoutes', null, {root: true})
        //   resolve(true)
        // }
      });
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}